package com.serkan.conversion;

import com.olcudegisim.islemler.*;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.test.IsolatedContext;
import android.view.ContextMenu;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.view.View.OnKeyListener;
import android.view.View.OnTouchListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

@SuppressLint("ShowToast")
public class ConversionActivity extends Activity {
	private Bundle extras;
	private ListView list;

	private double sayi = 1;
	private int groupPosition;
	private int childPosition;
	private TextView tx;
	private EditText eTxt;
	private Button OK;
	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_conversion);

		extras = getIntent().getExtras();

		groupPosition = extras.getInt("groupPosition");
		childPosition = extras.getInt("childPosition");

		tx = (TextView) findViewById(R.id.tx);
		tx.setText(extras.getString("olcu"));
		eTxt = (EditText) findViewById(R.id.editTxt);
		eTxt.setText("");
		list = (ListView) findViewById(R.id.list1);
		OK = (Button) findViewById(R.id.butOK);
		tx.setText(new olcuListeleme().childrenBirimElemaniDon(groupPosition,
				childPosition));
		listGirilenSayiyaGoreYenile();
		eTxt.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				onRestart();
				return false;
			}
		});
		
		OK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				sayi = 1;
				listGirilenSayiyaGoreYenile();

			}
		});
	}

	@Override
	protected void onRestart() {
		eTxt.clearFocus();
		eTxt.setText("");
		sayi = 1;
		listGirilenSayiyaGoreYenile();
		super.onRestart();

	}

	public void listGirilenSayiyaGoreYenile() {
		try {

			if (!String.valueOf(eTxt.getText()).equals("")) {
				sayi = Double.parseDouble(String.valueOf(eTxt.getText()));
			}
			list.setAdapter(adapterDon(sayi));
		} catch (Exception e) {
			Toast.makeText(ConversionActivity.this, "Hata : " + e.getMessage(), RESULT_OK);
		}
	}

	private ArrayAdapter<String> adapterDon(double sayi) {
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_list_item_1, this.kesinlistDon(this
						.listDouble_to_listString(this.listeDoldur(sayi,
								groupPosition, childPosition)), this
						.sirali_birimleri_yerlestir(groupPosition)));

		return adapter;
	}

	private List<String> kesinlistDon(List<String> sayiList,
			List<String> birimList) {
		List<String> son = new ArrayList<String>();
		for (int i = 0; i < sayiList.size(); i++) {
			son.add(sayiList.get(i) + " " + birimList.get(i));
		}

		return son;
	}

	private List<String> sirali_birimleri_yerlestir(int groupPosition) {
		List<String> listSBirimAdlari = null;
		try {
			String groupPositionString = new olcuListeleme()
					.groupElamaniDon(groupPosition);

			if (groupPositionString == "Length") {
				listSBirimAdlari = new olcuBirimIslemleri()
						.UzunlukBirimAdlariDon();
			} else if (groupPositionString == "Area") {
				listSBirimAdlari = new olcuBirimIslemleri()
						.AlanBirimAdlariniDon();
			} else if (groupPositionString == "Weigth") {
				listSBirimAdlari = new olcuBirimIslemleri()
						.AgirlikBirimAdlariniDon();
			} else if (groupPositionString == "Volume") {
				listSBirimAdlari = new olcuBirimIslemleri()
						.HacimBirimAdlariniDon();
			} else if (groupPositionString == "Temperature") {
				listSBirimAdlari = new olcuBirimIslemleri()
						.SicaklikBirimAdlariniDon();
			}

		} catch (Exception e) {
			Toast.makeText(ConversionActivity.this, "Hata:" + e.getMessage(),
					RESULT_OK);
		}
		return listSBirimAdlari;
	}

	public List<String> listDouble_to_listString(List<Double> listD) {
		List<String> listS = new ArrayList<String>();
		for (int i = 0; i < listD.size(); i++) {
			listS.add(String.valueOf(listD.get(i)));
		}
		return listS;
	}

	public List<Double> listeDoldur(double sayi, int groupPosition,
			int childPosition) {
		List<Double> listD = null;
		try {
			String groupPositionString = new olcuListeleme()
					.groupElamaniDon(groupPosition), childPositionString = new olcuListeleme()
					.childrenBirimElemaniDon(groupPosition, childPosition);

			if (groupPositionString == "Length") {
				listD = new olcuBirimIslemleri().uzunluk_cevir(sayi,
						childPositionString);
			} else if (groupPositionString == "Area") {
				listD = new olcuBirimIslemleri().alan_cevir(sayi,
						childPositionString);
			} else if (groupPositionString == "Weigth") {
				listD = new olcuBirimIslemleri().agirlik_cevir(sayi,
						childPositionString);
			} else if (groupPositionString == "Volume") {
				listD = new olcuBirimIslemleri().hacim_cevir(sayi,
						childPositionString);
			} else if (groupPositionString == "Temperature") {
				
				listD = new olcuBirimIslemleri().sicaklik_cevir(sayi,
						childPositionString);
			}

		} catch (Exception e) {
			Toast.makeText(ConversionActivity.this, "Hata:" + e.getMessage(),
					RESULT_OK);
		}
		return listD;
	}
}
